//
//  ResultContentCell.m
//  iiecg
//
//  Created by Shanlin on 2017/3/1.
//  Copyright © 2017年 Boundary Technology. All rights reserved.
//

#import "ResultContentCell.h"
#import "ReportDescPop.h"

@interface ResultContentCell ()

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentWConstraint;
@property (weak, nonatomic) IBOutlet UIImageView *problemImageView;
@property (weak, nonatomic) IBOutlet UIButton *descButton;

@property(nonatomic,strong)ResultContentBean *bean;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *valueWConstraint;

@end

@implementation ResultContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.problemImageView.image = [UIImage imageNamed:@"ecg_img_problem"];
    [self.descButton setImage:[UIImage imageNamed:@"ecg_haveNoPro"] forState:UIControlStateNormal];
    self.contentLabel.font = [UIFont systemFontOfSize:15];

}

-(void)setBean:(ResultContentBean *)bean
{
    __weak typeof(self)weakSelf = self;
    _bean = bean;
    if (!bean) {
        return;
    }
    [IKPublicUtil runOnMainThread:^{
        weakSelf.problemImageView.hidden = YES;
        weakSelf.contentWConstraint.constant = [bean widthForString:bean.text fontSize:16];
        weakSelf.contentLabel.text = bean.text;
        weakSelf.contentLabel.textColor = COLOR_BLACK_6F6F6F;
        if (bean.text.length > 0 )
        {
            weakSelf.contentLabel.attributedText = [weakSelf attributeInterchange:bean.text partOneColor:COLOR_BLACK_252525 andFont:[UIFont systemFontOfSize:16]];
        }
        if (bean.problem)
        {
            [weakSelf.descButton setImage:[UIImage imageNamed:@"ecg_havePro"] forState:UIControlStateNormal];
        }
        
        weakSelf.valueLabel.text = bean.value;
        weakSelf.valueWConstraint.constant = [bean widthForString:bean.value fontSize:15];
        weakSelf.descButton.hidden = bean.isHiddenDesc;
        if (bean.object && [bean.object isKindOfClass:[NSMutableAttributedString class]]) {
            weakSelf.contentLabel.attributedText = bean.object;
        }
    }];
}

+(instancetype)createWith:(UITableView *)tableView bean:(ResultContentBean *)bean
{
    static NSString *ID = @"ResultContentCell";
    ResultContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:ID owner:nil options:nil] lastObject];
    } else {
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.bean = bean;
    return cell;

}

- (IBAction)descAction:(UIButton *)sender {
    [[ReportDescPop createWith:self.bean.descTitle desc:self.bean.desc] show:YES];
}

- (NSMutableAttributedString *)attributeInterchange:(NSString *)text partOneColor:(UIColor *)color andFont:(UIFont *)font
{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    NSArray *items = [text componentsSeparatedByString:@":"];
    
    NSString *firstString = items.firstObject;
    
    NSInteger firstLength = firstString.length;
    
    NSRange range = NSMakeRange(firstLength, text.length - firstLength);
    
    [noteStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    [noteStr addAttribute:NSFontAttributeName value:font range:range];
    
    return noteStr;

}


@end
